﻿// *****************************************************************************
// **                   Ladenius MediaCenter TVSeries                         **
// ** ----------------------------------------------------------------------- **
// ** (c) 2008 by Jurjen Ladenius                                             **
// *****************************************************************************

#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ladenius.Series.Path;
using Effectory.Utilities;
using Ladenius.Series.Path.Entities;
using JesseJohnston;
using System.Collections.ObjectModel;
using System.IO;
using Ladenius.Series;
using Ladenius.Series.Business;
#endregion

namespace Ladenius.Series.Configuration.WinClient {
    public partial class frmMain : Form {

        string languageAbbreviation = "en";
        public ObjectListView<ParsedFile> _parsedFiles = new ObjectListView<ParsedFile>(new List<ParsedFile>());

        #region Constructor
        public frmMain() {
            InitializeComponent();
        }
        #endregion

        #region Methods - Parsing 
        /// <summary>
        /// Parses the file system.
        /// </summary>
        internal void ParseFileSystem() {

            // Get the base paths
            List<string> basePaths = new List<string>();
            foreach (string path in lstImportPaths.Items) {
                basePaths.Add(path);
            }

            // Get the parsing rules
            List<string> parsingRules = new List<string>();
            foreach (string rule in lstImportRules.Items) {
                parsingRules.Add(rule);
            }

            PathManager pathManager = new PathManager(basePaths, parsingRules);
            pathManager.ReportProgress += new PathManager.PathProgressEventHandler(pathManager_ReportProgress);
            pathManager.Completed += new PathManager.CompletedEventHandler(pathManager_Completed);
            pathManager.Parse();


        }

        /// <summary>
        /// Occurs when the pathManager completed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        private void pathManager_Completed(object sender) {

            

            // Retrieve parsed files
            PathManager pathManager = (PathManager)sender;
            ReadOnlyCollection<ParsedFile> parsedFiles = pathManager.ParsedFiles;

            DisplayProgress(int.MinValue, "Started importing parsed files.");

            // TODO: Parse asynchronous

            // Add these parsed files to the database
            Parser parser = new Parser();
            parser.ProgressReported += new Parser.ProgressReportedEventHandler(parser_ProgressReported);
            parser.ParseSeries(parsedFiles, languageAbbreviation);

            DisplayProgress(int.MinValue, "Finished importing parsed files.");

            foreach (Control control in pgImport.Controls) control.Enabled = true;

        }

        /// <summary>
        /// Reports the parser's progress
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="message">The message.</param>
        private void parser_ProgressReported(object sender, string message) {
            DisplayProgress(int.MinValue, message);
        }
        #endregion

        #region Methods - UI

        #region UI Delegates
        private delegate void UIMethod<T, U> (T t, U u);
        #endregion

        /// <summary>
        /// Displays the progress.
        /// </summary>
        /// <param name="percentage">The percentage.</param>
        /// <param name="message">The message.</param>
        private void DisplayProgress(int percentage, string message) {
            if (InvokeRequired) {
                BeginInvoke(new UIMethod<int, string>(DisplayProgress), percentage, message);
                return;
            }

            if (percentage > 0) {
                // TODO: Do something with the progress percentage 
            }
            txtLog.Text += message + Environment.NewLine;
        }
        #endregion

        #region Events
        /// <summary>
        /// Handles the Click event of the btnImportPathAdd control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnImportPathAdd_Click(object sender, EventArgs e) {
            // Add a path to the list of import paths
            string path = txtImportPathAdd.Text.Trim();
            if (new DirectoryInfo(path).Exists) {
                lstImportPaths.Items.Add(path);
                txtImportPathAdd.Text = string.Empty;
            }
        }

        /// <summary>
        /// Handles the Click event of the btnImportPathBrowse control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnImportPathBrowse_Click(object sender, EventArgs e) {
            dlgImportPath.SelectedPath = txtImportPathAdd.Text;
            DialogResult res = dlgImportPath.ShowDialog(this);
            if (res == DialogResult.OK) {
                txtImportPathAdd.Text = dlgImportPath.SelectedPath;
            }  else {
                MessageBox.Show("Select another path and try again.", "Path does not exist.", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                txtImportPathAdd.Text = string.Empty;
            }
        }

        /// <summary>
        /// Handles the Click event of the btnImportRuleAdd control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnImportRuleAdd_Click(object sender, EventArgs e) {
            // TODO: Add more validation
            string rule = txtImportRuleAdd.Text.Trim();
            if (rule != string.Empty) {
                lstImportRules.Items.Add(rule);
                txtImportRuleAdd.Text = string.Empty;
            }
        }

        /// <summary>
        /// Handles the Click event of the btnImportPathRemove control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnImportPathRemove_Click(object sender, EventArgs e) {
            if (lstImportPaths.SelectedItem != null)
                lstImportPaths.Items.Remove(lstImportPaths.SelectedItem);
        }

        /// <summary>
        /// Handles the Click event of the btnImportRuleRemove control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnImportRuleRemove_Click(object sender, EventArgs e) {
            if (lstImportRules.SelectedItem != null)
                lstImportRules.Items.Remove(lstImportRules.SelectedItem);

        }
        /// <summary>
        /// Handles the ReportProgress event of the pathManager control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Ladenius.Series.Path.Entities.PathProgressEventArgs"/> instance containing the event data.</param>
        private void pathManager_ReportProgress(object sender, PathProgressEventArgs e) {
            DisplayProgress(e.Percentage, e.Message);
        }
        /// <summary>
        /// Handles the Click event of the btnImportStart control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnImportStart_Click(object sender, EventArgs e) {
            foreach (Control control in pgImport.Controls) control.Enabled = false;
            ParseFileSystem();
        }
        #endregion

        private void button1_Click(object sender, EventArgs e) {
         
        }


    }
}
